<template>
  <div class="bg">
    <div class="content">
      <div class="left">
        <div class="main">
          <div class="struct">目录结构</div>
          <div class="el-struct">
            <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="main">
          <div class="add">
            <el-button type="primary" @click="dialogFormVisible = true" size="small" icon="el-icon-circle-plus-outline">
              添加{{ buttomName }}权限
            </el-button>
            <el-dialog :title="`新增权限：${buttomName}->`" :visible.sync="dialogFormVisible" @close="close">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="权限名" prop="name">
                  <el-input v-model="ruleForm.name" placeholder="请输入权限名"></el-input>
                </el-form-item>
                <el-form-item label="Code" prop="code">
                  <el-input v-model="ruleForm.code" placeholder="请输入权限Code"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="describe">
                  <el-input type="textarea" v-model="ruleForm.describe"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="ruleForm.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="ruleForm.state">
                    <el-option label="启用" value="启用"></el-option>
                    <el-option label="禁用" value="禁用"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="赋予" prop="give">
                  <el-checkbox-group v-model="ruleForm.give">
                    <el-checkbox label="超级管理员" name="type"></el-checkbox>
                    <el-checkbox label="普通管理员" name="type"></el-checkbox>
                    <el-checkbox label="运维人员" name="type"></el-checkbox>
                  </el-checkbox-group>
                </el-form-item>

              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">OK</el-button>
              </div>
            </el-dialog>
          </div>
          <div class="el-table">
            <el-table :data="tableData" style="width: 100%" row-class-name>
              <el-table-column type="index" label="序号">
              </el-table-column>
              <el-table-column prop="name" label="权限名称">
              </el-table-column>
              <el-table-column prop="code" label="Code">
              </el-table-column>
              <el-table-column prop="describe" label="描述" width="200">
              </el-table-column>
              <el-table-column prop="state" label="状态">
              </el-table-column>
              <el-table-column label="操作" width="120">
                <template slot-scope="scope">
                  <div class="icon">
                    <div @click="look(scope.$index, scope.row)" class="first"><i class="el-icon-view"></i></div>

                    <div @click="updata(scope.$index)" class="child2"><i class="el-icon-setting"></i></div>
                    <div>
                      <el-popconfirm title="确定删除吗？" placement="top" @confirm="handleDelete(scope.$index)">
                        <div slot="reference"><i class="el-icon-delete"></i></div>
                      </el-popconfirm>
                    </div>

                  </div>
                </template>

              </el-table-column>
            </el-table>
            <el-dialog :title="`新增权限：${buttomName}->查看`" :visible.sync="lookDialogFormVisible">
              <el-form :model="lookData" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="权限名" prop="name">
                  <el-input v-model="lookData.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="Code" prop="code">
                  <el-input v-model="lookData.code" disabled></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="describe">
                  <el-input type="textarea" v-model="lookData.describe" disabled></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="lookIndex" disabled></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="lookData.state" disabled>
                    <el-option label="启用" value="启用" disabled></el-option>
                    <el-option label="禁用" value="禁用" disabled></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="赋予" prop="give">
                  <el-checkbox-group v-model="lookData.give" disabled>
                    <el-checkbox label="超级管理员" name="type" disabled></el-checkbox>
                    <el-checkbox label="普通管理员" name="type" disabled></el-checkbox>
                    <el-checkbox label="运维人员" name="type" disabled></el-checkbox>
                  </el-checkbox-group>
                </el-form-item>

              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="lookDialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="lookDialogFormVisible = false">OK</el-button>
              </div>
            </el-dialog>

            <!-- 编辑更新 -->
            <el-dialog :title="`新增权限：${buttomName}->查看`" :visible.sync="updateDialogFormVisible">
              <el-form :rules="rules" :model="updateData" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
                <el-form-item label="权限名" prop="name">
                  <el-input v-model="updateData.name"></el-input>
                </el-form-item>
                <el-form-item label="Code" prop="code">
                  <el-input v-model="updateData.code"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="describe">
                  <el-input type="textarea" v-model="updateData.describe"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="updateData.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="updateData.state">
                    <el-option label="启用" value="启用"></el-option>
                    <el-option label="禁用" value="禁用"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="赋予" prop="give">
                  <el-checkbox-group v-model="updateData.give">
                    <el-checkbox label="超级管理员" name="type"></el-checkbox>
                    <el-checkbox label="普通管理员" name="type"></el-checkbox>
                    <el-checkbox label="运维人员" name="type"></el-checkbox>
                  </el-checkbox-group>
                </el-form-item>

              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="updateDialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="updateed('ruleForm1')">OK</el-button>
              </div>
            </el-dialog>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
import data from '../../data/data.js'
export default {
  data() {
    return {
      //添加 弹窗
      dialogFormVisible: false,

      // 查看 弹窗
      lookDialogFormVisible: false,
      lookData: [],
      lookIndex: null,

      // 编辑
      updateDialogFormVisible: false,
      updateData: {
        name: '',
        code: '',
        describe: '',
        sort: null,
        state: '启用',
        give: []
      },
      updataIndex: null,

      ruleForm: {
        name: '',
        code: '',
        describe: '',
        sort: null,
        state: '启用',
        give: []
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^(?:[1-9]\d*)$/.test(value) == true || value === null) {
                callback();
              } else {
                callback(new Error("请输入正整数"));
              }
            },
            trigger: "change",
          },
        ]
      },


      buttomName: '首页',
      data: data.permissions,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      tableData: []
    }
  },
  methods: {
    // 表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          this.dialogFormVisible = false;
          let dataObj = {}
          // this.$set(对象名,“属性名”,属性值)
          this.$set(dataObj, 'name', this.ruleForm.name)
          this.$set(dataObj, 'code', this.ruleForm.code)
          this.$set(dataObj, 'describe', this.ruleForm.describe)
          this.$set(dataObj, 'sort', this.ruleForm.sort)
          this.$set(dataObj, 'state', this.ruleForm.state)
          this.$set(dataObj, 'give', this.ruleForm.give)

          // 数组.push(元素1,元素2,元素3.....元素n);
          // 数组.splice(index,howmany,item1,.....,itemN);
          if (dataObj.sort > this.tableData.length) {
            this.tableData.push(dataObj);
          } else {
            this.tableData.splice(dataObj.sort - 1, 0, dataObj);
          }

          // console.log(this.tableData);
          this.$message({
            message: '添加成功',
            type: 'success'
          });

          this.ruleForm.name = ''
          this.ruleForm.code = ''
          this.ruleForm.describe = ''
          this.ruleForm.sort = null
          this.ruleForm.state = '启用'
          this.ruleForm.give = []

        } else {
          console.log(111);
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    close() {
      this.ruleForm.name = ''
      this.ruleForm.code = ''
      this.ruleForm.describe = ''
      this.ruleForm.sort = null
      this.ruleForm.state = '启用'
      this.ruleForm.give = []
    },
    handleNodeClick(data) {
      this.tableData = data.tableData
      this.buttomName = data.name
    },

    // 删除
    handleDelete(index) {
      console.log(index);
      this.tableData.splice(index, 1)
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
    },

    look(index, row) {
      this.lookDialogFormVisible = true;
      this.lookData = row
      this.lookIndex = index + 1

    },

    updata(index) {
      this.updateDialogFormVisible = true;
      this.updateData.name = this.tableData[index].name
      this.updateData.code = this.tableData[index].code
      this.updateData.describe = this.tableData[index].describe
      this.updateData.sort = index + 1
      this.updateData.state = this.tableData[index].state
      this.updateData.give = this.tableData[index].give

      this.updataIndex = index

    },

    updateed(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateDialogFormVisible = false;
          let dataObj = {};
          // this.$set(对象名,“属性名”,属性值)
          this.$set(dataObj, 'name', this.updateData.name)
          this.$set(dataObj, 'code', this.updateData.code)
          this.$set(dataObj, 'describe', this.updateData.describe)
          this.$set(dataObj, 'sort', this.updateData.sort)
          this.$set(dataObj, 'state', this.updateData.state)
          this.$set(dataObj, 'give', this.updateData.give)

          // 数组.push(元素1,元素2,元素3.....元素n);
          // 数组.splice(index,howmany,item1,.....,itemN);
          console.log(dataObj.sort);
          this.tableData.splice(this.updataIndex, 1);
          if (dataObj.sort > this.tableData.length) {
            this.tableData.push(dataObj);
          } else {
            this.tableData.splice(dataObj.sort - 1, 0, dataObj);
          }

          // console.log(this.tableData);
          this.$message({
            message: '修改成功',
            type: 'success'
          });

          this.updateData.name = '',
            this.updateData.code = '',
            this.updateData.describe = '',
            this.updateData.sort = null,
            this.updateData.state = '启用',
            this.updateData.give = []

        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>
.bg {
  background-color: #fff;
  height: calc(100vh - 100px);

  .content {
    // height: calc(100vh - 100px);
    width: 100%;
    // float: left;
    background-color: #fff;
    // padding: 16px;
    // overflow: auto;
    display: flex;

    .left {
      // float: left;
      // background-color: pink;
      padding: 16px;
      width: 256px;
      height: inherit;

      .main {
        width: 256px;
        min-height: 272px;
        border: 1px #f0f0f0 solid;
        height: 100%;

        .struct {
          height: 44px;
          border-bottom: 1px #f0f0f0 solid;
          padding: 0 10px;
          font-size: 14px;
          color: #000000;
          line-height: 44px;

          .el-struct {
            width: 100%;
          }
        }
      }
    }

    .right {
      // float: left;
      width: calc(100% - 304px);
      padding: 16px 16px 16px 0;
      // background-color: skyblue;
      width: 100%;
      flex: 1;
      overflow: hidden;

      .main {

        width: calc(100% - 5px);
        min-height: 272px;
        border: 1px #f0f0f0 solid;
        overflow: hidden;

        .add {
          height: 30px;
          width: 100%;
          padding: 10px;
        }

        .el-table {
          min-height: 222px;

          .icon {
            width: 80px;
            height: 15px;
            display: flex;

            div {
              height: 15px;
              flex: 1;
              font-size: 14px;
              text-align: center;
              line-height: 15px;
              cursor: pointer;
            }

            .first {
              color: #00a854;
              border-right: 1px #f0f0f0 solid;
            }

            .child2 {
              color: #0066cc;
              border-right: 1px #f0f0f0 solid;
            }

            div:last-child {
              color: #ff3333;
            }
          }
        }
      }
    }
  }
}
</style>